﻿<%@ Page Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" CodeFile="InputStore.aspx.cs"
    Inherits="Manager_ProductStore_InputStore" Title="Untitled Page" %>

<%@ Register Src="../../Pub/Module/Calendar_Submit.ascx" TagName="Calendar" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <!--<link href="<%= ResolveClientUrl("~/")%>Pub/Css/JQ/jquery.autocomplete.css" rel="stylesheet"
        type="text/css" />-->

    <script src="<%= ResolveClientUrl("~/")%>Pub/Js/JQ/jquery.autocomplete.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
   
               var Url='<%= ResolveClientUrl("~/")%>';
               function fnDisplayTextMoneyPrice(obj)
                {
                    obj.value=FormatMoney(obj.value.ReplaceAll(",",""))   ;                    
                }  
              
              
              
              $(document).ready(function() 
                {   
                         $str='<%=sData %>';   
                         data = eval("(" + $str + ')');
                         var $obj;    
                         BindAutoComplete($(".txtPDName"));
                         function BindAutoComplete($objInput)  
                         {
                               $objInput.autocomplete(data, {
                                    minChars: 1,
		                            width: 500,		      
		                            selectFirst: true,
		                          //   autoFill: true,
                                  formatItem: function(item) {                           
                                    return item.PDName;
                                  }
                                }).result(function(event, item) {  
                                    if(item.PDID!="")      
                                    { 
                                        $(this).next().val(item.PDID);
                                        $(this) .css("background-color","LightGoldenrodYellow");
                                     } 
                                    else
                                     { 
                                          $(this).next().val("");  
                                      }
                                }).keyup(function(e){
                                      if(e.keyCode <9 ||  e.keyCode >45 ) 
                                      {
                                          $(this) .css("background-color","white");
                                          $(this).next().val("");  
                                       } 
                                        });      
                                            
                                 
                             }       
                            
                            //////////////////
                            
                            $(".txtCalcTotal").live("keyup",function(){                                  
                                 CalcTotalRow($(this))
                            });
                             $(".ddlRate").live("change",function(){                                  
                                 CalcTotalRow($(this))
                            });
                            function CalcTotalRow($obj)
                            {
                                var sRate=1;
                                $objQuantity=$obj.parent().parent().find("td").eq(4).children(0);
                                $objPrice=$obj.parent().parent().find("td").eq(5).children(0);  
                                $objRate=$obj.parent().parent().find("td").eq(5).find(".ddlRate");                                      
                                if($objRate.val()=="USD") 
                                      sRate=$(".lblRateToday").text().ReplaceAll(",","");                                              
                                $objTotal=$obj.parent().parent().find("td").eq(6).children(0);   
                                $strTotal= $objQuantity.val().ReplaceAll(",","")*$objPrice.val().ReplaceAll(",","")*sRate;     
                                $strTotal = FormatMoney($strTotal);
                                $objTotal.text($strTotal);
                            }
                            $(".imgNewRow").click(function(){                       
                                       $('#<%=grvMaster.ClientID %> tr:last').after("<tr>"+$('#<%=grvMaster.ClientID %> tr:last').html()+"</tr>")
                                       $row=$('#<%=grvMaster.ClientID %> tr:last');
                                       
                                       $row.find("td").eq(0).find(".hidStDID").val("");
                                       $row.find("td").eq(1).text($('#<%=grvMaster.ClientID %> > tbody > tr').length-1);
                                       //====
                                       txtPDName=$row.find("td").eq(2).find(".txtPDName");
                                       $row.find("td").eq(2).find(".hidPDID").val("");
                                       txtPDName.attr("class","txtPDName");
                                       txtPDName.val("");
                                       txtPDName.css("background-color","white");
                                       BindAutoComplete(txtPDName);           
                                       //====
                                       $row.find("td").eq(3).find(".ddlStore").get(0).selectedIndex=0;       
                                       //====
                                       $row.find("td").eq(4).find(".txtCalcTotal").val("1");
                                       //====
                                       $row.find("td").eq(5).find(".txtCalcTotal").val("0");       
                                       $row.find("td").eq(5).find(".ddlRate").get(0).selectedIndex=0;     
                                       //====
                                       $row.find("td").eq(6).find(".lblTotal").text("0");             
                            });
                            
                            
                             $(".imgDelRow").live("click",function()
                             {         
                                    if($('#<%=grvMaster.ClientID %> > tbody > tr').length<=2)
                                    {
                                        alert("Could not remove more") ;
                                        return;  
                                    }  
                                  if(confirm("Do you want to delete ???"))  
                                  {                                  
                                      $rows=$(this).parent().parent();              
                                      StDID=$rows.find("td").eq(0).find(".hidStDID").val();
                                      if(StDID!="" && StDID!="0")
                                      {
                                      
                                            $strXML="<PccMsg>";
                                            $strXML+="<Method>DELETE</Method>";  
                                            $strXML+="<StDID>"+StDID+"</StDID>";                                         
                                            $strXML+="</PccMsg>";                                    
                                            $("#<%= hidXML.ClientID %>").val($strXML);
                                            document.getElementById("aspnetForm").submit();       
                                      }
                                      else
                                      {
                                            $rows.remove();    
                                      }                                                                                                               
                                   }  
                              });    
                         
                             $("#btnSave").live("click",function()
                             {       
                                    if('<%= ViewState["DisCompID"] %>'=="" ) 
                                    {
                                     
                                        alert("Chọn nhà cung cấp") ;
                                        return false;
                                     }
                                    bErr="0"  ;
                                    $strXML="<PccMsg>";
                                    $strXML+="<Method>SAVE</Method>";
                                    
                                    //For
                                   $('#<%=grvMaster.ClientID %> > tbody > tr').each(function() 
                                   {
                                        index=this.rowIndex;
                                        
                                       if (!index && index==0) return; 
                                        $row=$(this);
                                        
                                        StDID=$row.find("td").eq(0).find(".hidStDID").val();
                                        PDID=$row.find("td").eq(2).find(".hidPDID").val();
                                        StID=$row.find("td").eq(3).find(".ddlStore").val();
                                        Quantity=$row.find("td").eq(4).find(".txtCalcTotal").val();
                                        InputPrice=$row.find("td").eq(5).find(".txtCalcTotal").val();                                       
                                        InputRate=$row.find("td").eq(5).find(".ddlRate").val();
                                        Total=$row.find("td").eq(6).find(".lblTotal").text();
                                        
                                        var sErr="STT: "+index+"-->";
                                         //check
                                         if(PDID=="")
                                         {
                                                bErr="1";
                                                alert(sErr+" chọn sản phẩm");
                                                return false;
                                         }  
                                         else if(Quantity<=0)
                                         {
                                                bErr="1";
                                                alert(sErr+" Nhập số lượng");
                                                return false;
                                         } 
                                        else if(InputPrice<=0)
                                         {
                                                bErr="1";
                                                alert(sErr+" Nhập đơn giá");
                                                return false;
                                         }    
                                        
                                         $strXML+="<StoreDetail>";
                                         $strXML+="<StDID>"+StDID+"</StDID>";
                                         $strXML+="<PDID>"+PDID+"</PDID>";
                                         $strXML+="<StID>"+StID+"</StID>"; 
                                         $strXML+="<Quantity>"+Quantity+"</Quantity>"; 
                                         $strXML+="<InputPrice>"+InputPrice+"</InputPrice>";
                                         $strXML+="<InputRate>"+InputRate+"</InputRate>";                                         
                                         $strXML+="<Total>"+Total+"</Total>";             
                                         $strXML+="</StoreDetail>";
                                    });     
                                   
                                    $strXML+="</PccMsg>";  
                                                                      
                                    if(bErr=="1")  return false;
                                    
                                   // alert($strXML)
                                     
                                    
                                   $("#<%= hidXML.ClientID %>").val($strXML);
                                   document.getElementById("aspnetForm").submit();                          
                        });              
                             
                });   //end document.ready
               
               function fnReloadPage()
               {
                   // ("form:first").submit();
                   document.getElementById("<%= hidXML.ClientID %>").value="<PccMsg><Method>LOADDATA</Method></PccMsg>";
                    document.getElementById("aspnetForm").submit();
               } 
               
                         
            
    </script>

    <table border="0" cellpadding="2" cellspacing="0" width="100%">
        <tr>
            <td>
                <table border="1" cellpadding="0" cellspacing="0" style="width: 100%; border-collapse: collapse"
                    bordercolor="#407db3">
                    <tr>
                        <td style="width: 80%">
                            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tr>
                                    <td valign="top">
                                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" ShowMessageBox="True"
                                            ShowSummary="False" ValidationGroup="grCustomer" />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" valign="middle">
                                        <table border="0" cellpadding="1" cellspacing="0" style="width: 100%">
                                            <tr>
                                                <td align="right" class="td_GuestInfo" nowrap="nowrap" style="width: 87px">
                                                    Tên:</td>
                                                <td align="left" colspan="3" nowrap="nowrap">
                                                    <asp:TextBox ID="txtHoTen" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                        ReadOnly="True" Width="85%"></asp:TextBox>
                                                 
                                            
                                                    <img src="../../Images/Icon/application get Icon.jpg"
                                                        style="cursor: pointer"  width="16"  onclick="OpenDistributionCompany()"/></td>
                                            </tr>
                                            <tr style="color: #000000">
                                                <td align="right" class="td_GuestInfo" nowrap="nowrap" style="width: 87px">
                                                    Địa chỉ:</td>
                                                <td align="left" colspan="3" nowrap="nowrap">
                                                    <asp:TextBox ID="txtDiaChi" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                        ReadOnly="True" ValidationGroup="grCustomer" Width="90%"></asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr style="color: #000000">
                                                <td align="right" class="td_GuestInfo" nowrap="nowrap" style="width: 87px">
                                                    Điện thoại:</td>
                                                <td align="left" nowrap="nowrap">
                                                    <asp:TextBox ID="txtDienThoai" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                        ReadOnly="True" ValidationGroup="grCustomer" Width="150px"></asp:TextBox>
                                                    &nbsp;
                                                </td>
                                                <td align="left" nowrap="nowrap" style="width: 10%">
                                                    <asp:Label ID="Label2" runat="server" Text="Mã số thuế:"></asp:Label></td>
                                                <td align="left" nowrap="nowrap">
                                                    <asp:TextBox ID="txtTaxNo" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                        ReadOnly="True" ValidationGroup="grCustomer"></asp:TextBox></td>
                                            </tr>
                                            <tr>
                                                <td align="right" class="td_GuestInfo" nowrap="nowrap" style="width: 87px">
                                                    Ghi chú</td>
                                                <td align="left" colspan="3" nowrap="nowrap">
                                                    <asp:TextBox ID="txtNote" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                        ReadOnly="True" ValidationGroup="grCustomer" Width="90%"></asp:TextBox></td>
                                            </tr>
                                            <tr>
                                                <td align="left" colspan="4" nowrap="nowrap">
                                                </td>
                                            </tr>
                                        </table>
                                        <br />
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td>
                            <table border="0" cellpadding="2" cellspacing="0" style="width: 100%">
                                <tr>
                                    <td align="right">
                                        Ngày:</td>
                                    <td>
                                        <uc1:Calendar ID="Calendar1" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        Tỉ giá:</td>
                                    <td>
                                        <asp:Label ID="lblRateToday" runat="server" Text="0" CssClass="lblRateToday"></asp:Label>
                                        <asp:Label ID="Label1" runat="server" Text="VNĐ"></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <asp:GridView ID="grvMaster" runat="server" AutoGenerateColumns="False" BorderColor="#407db3"
                    BorderWidth="1px" CellPadding="2" PageSize="20" Width="100%" OnRowDataBound="grvMaster_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                Xóa
                            </HeaderTemplate>
                            <ItemTemplate>
                                <input type="hidden" class="hidStDID" id="hidStDID" runat="server" />
                                <img src="../../Images/Icon/cmdDelete.gif" class="imgDelRow" stdid='<%# Eval("StDID") %>' /></HeaderTemplate>
                            </ItemTemplate>
                            <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Width="1%" Wrap="False" />
                            <HeaderStyle CssClass="cssBorderGrv" Wrap="False" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <img src="../../Images/Icon/add_icon.png" class="imgNewRow" /></HeaderTemplate>
                            <ItemTemplate>
                                <%#Container.DisplayIndex + 1%>
                            </ItemTemplate>
                            <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Width="1%" Wrap="False" />
                            <HeaderStyle CssClass="cssBorderGrv" Wrap="False" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                Tên Hàng</HeaderTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="txtPDName" runat="server" Width="99%" class="txtPDName"></asp:TextBox>
                                <input type="hidden" runat="server" id="hidPDID" class="hidPDID" />
                            </ItemTemplate>
                            <ItemStyle CssClass="cssBorderGrv" Wrap="False" HorizontalAlign="Center" />
                            <HeaderStyle CssClass="cssBorderGrv" Width="50%" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                Kho hàng</HeaderTemplate>
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlStore" runat="server" CssClass="ddlStore">
                                </asp:DropDownList>
                            </ItemTemplate>
                            <ItemStyle CssClass="cssBorderGrv" Wrap="False" HorizontalAlign="Center" />
                            <HeaderStyle CssClass="cssBorderGrv" Width="5%" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                Số lượng</HeaderTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="txtQuantity" runat="server" Text="1" Width="50px" Style="text-align: right"
                                    CssClass="txtCalcTotal" onkeyup="fnDisplayTextMoneyPrice(this)"></asp:TextBox>
                            </ItemTemplate>
                            <ItemStyle CssClass="cssBorderGrv" Wrap="False" HorizontalAlign="Center" />
                            <HeaderStyle CssClass="cssBorderGrv" Width="2%" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                Đơn giá</HeaderTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="txtPrice" runat="server" Style="text-align: right" Text="0" CssClass="txtCalcTotal"
                                    onkeyup="fnDisplayTextMoneyPrice(this)"></asp:TextBox>
                                <asp:DropDownList ID="ddlRate" runat="server" CssClass="ddlRate">
                                    <asp:ListItem>VNĐ</asp:ListItem>
                                    <asp:ListItem>USD</asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                            <ItemStyle CssClass="cssBorderGrv" Wrap="False" HorizontalAlign="Center" />
                            <HeaderStyle CssClass="cssBorderGrv" Width="10%" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                Thành tiền (VNĐ)
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblTotal" runat="server" Style="text-align: right" Text="0" CssClass="lblTotal"></asp:Label>
                            </ItemTemplate>
                            <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Width="25%" />
                            <HeaderStyle CssClass="cssBorderGrv" />
                        </asp:TemplateField>
                    </Columns>
                    <RowStyle CssClass="gvRowStyle" Height="10px" />
                    <HeaderStyle CssClass="cssHeader" Height="20px" HorizontalAlign="Center" Wrap="False" />
                    <AlternatingRowStyle CssClass="gvgRowStyle" Height="10px" />
                </asp:GridView>
            </td>
        </tr>
        <tr>
            <td style="background-color: whiteSmoke; height: 20px; text-align: center; vertical-align: middle">
                <input type="button" class="cssButton" value="Bảo lưu" style="width: 80px" id="btnSave" />
            </td>
        </tr>
    </table>
    <input type="hidden" id="hidXML" runat="server" />

    
    <script language="javascript" type="text/javascript">
    function fnDistributionCompany(DisCompID)
    {
              if(DisCompID!="" && DisCompID!="0")
              {
                  $(document).ready(function() 
                    {
                        $strXML="<PccMsg>";
                        $strXML+="<Method>LOADDISTRIBUTIONCOMPANY</Method>";  
                        $strXML+="<DisCompID>"+DisCompID+"</DisCompID>";                                         
                        $strXML+="</PccMsg>";                                    
                        $("#<%= hidXML.ClientID %>").val($strXML);
                        document.getElementById("aspnetForm").submit();     
                      });  
              }
    }
    
     function OpenDistributionCompany()
    {
         window.open("../DistributionCompany/ChooseDistributionCompany.aspx");
    }    
    </script>
</asp:Content>
